" 1.定义列表
let animals = ['cat','dog','parrot']

" 2.通过索引获取列表中的元素
let cat = animals[0]
let dog = animals[1]
let parrot = animals[-1]

echo cat
echo dog
echo parrot

" 3.用切片获取子列表
" 与python 的主要区别在于vim的索引区间包含结尾索引
let slice = animals[1:]

echo slice

let slice = animals[0:1]

echo slice

" 4.1.使用add方法在列表末尾添加元素
" 函数单独调用需要在函数前加 call
call add(animals, 'octopus')

echo animals

" 4.2.会把修改后的内容返回
let far = add(animals,'duck')

echo far


" 5.1.insert 在列表前插入元素
call insert(animals, "monkey")
echo animals
" 5.2.insert 支持可选的索引参数，例如将'raven'添加到animals列表的索引为2的位置之前
call insert(animals, 'raven',2)
echo animals

" 6.1.unlet删除列表元素
unlet animals[2]
echo animals
" 6.2.使用remove删除列表元素
call remove(animals, -1)
echo animals
" 6.3.remove返回删除的元素
let bocat = remove(animals, 0)
echo bocat
" 6.4.unlet删除索引区间
" 删除0，1两个元素
unlet animals[:1]
echo animals
" 6.5.remove删除索引区间
call remove(animals, 0,1)
echo animals

" 7.1.列表拼接，使用+
let mammals = ['dog','cat']
let birds = ['raven', 'parrot']
let animals = mammals + birds
echo animals
" 7.2.使用 extend 函数扩展已有列表
call extend(mammals, birds)
echo mammals

" 8.列表排序，会修改列表内容
call sort(animals)
echo animals

" 9.找出元素的索引
let i = index(animals, 'parrot')
echo i

" 10.检查列表是否为空
if empty(animals)
    echo 'There aren''t any animal'
endif

let create = []

if empty(create)
    echo 'There aren''t any create'
endif

" 11.获得列表的长度
echo 'There are ' . len(animals) . ' animals'

" 12.统计列表中某元素的个数
echo 'There are ' . count(animals, 'cat') . ' cats here.'

" 获取帮助 :help list
